/*
 *   Copyright (C) 2007 by Andrzej Oczkowicz
 *   andrew_lz@poczta.fm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *   KMediaCatalog :)
 *
 */
#ifndef KSEARCH_H
#define KSEARCH_H

#include <QObject>
#include <QWidget>
#include <QDialog>
#include <QFileInfo>
#include <QThread>
#include <QTime>

#include ".ui/ui_dscan.h"

/**
	@author Andrzej Oczkowicz <andrew_lz@poczta.fm>
*/

class kSearch;
class scanThread;
class dkSearch;
class kMaster;
class kModMgr;

class kSearch : public QObject
{
Q_OBJECT

public:
   typedef struct scanStats{
      int  files;
      int  dirs;
      int  links;
      int  thumbs;
      int  descs;
   };

   typedef struct scanOptions{
      bool terminated;
      bool writing;
      bool done;
      scanThread * thread;
      kMaster * master;
      QString current;
      QString searchRoot;
      struct scanStats stats;
   };


public:
    kSearch(QObject *parent = 0);
   ~kSearch();

public:
    scanOptions data;
    dkSearch *dlgSearch;

public:
    void showFile( QString &file, bool is_dir = false );
    int scanDirectory(const QString &dir);

private:
    kModMgr * mgr;

private:
    int onDir(QString &dir, bool &terminated );
    int processFile(QFileInfo *fi );
    void findAllFiles(QString &dir, bool &terminated );
    int scAdd( QString &file );

    friend class scanThread;
};

class scanThread: public QThread
{
  private:
    bool _do_term;
    kSearch *ksearch;

  public:
    virtual void run();
    bool stop( int waittime = 0 );
    scanThread( kSearch *owner );
};


class dkSearch : public QDialog, private Ui::formScanning
{
  Q_OBJECT
  public:
    QTimer *statTimer;
    QPushButton *btn;

  private:
    kSearch::scanOptions *scanData;
    QTime start;

  public:
    void setButtonType(bool cancel = true);
    void updateStats();
    void closeEvent(QCloseEvent *e);


  public slots:
    void statTimer_timeout();
    void on_buttonOk_clicked( bool checked=false );

  public:
     dkSearch(kSearch::scanOptions &data);
    ~dkSearch();
};


#endif
